
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace SariturIntranetLibrary.Noticias
{
    [Serializable()]
    public class NoticiaChildInfo : Csla.ReadOnlyBase<NoticiaChildInfo>
    {

        #region Business Properties and Methods

        //declare members

        private Guid _id = Guid.NewGuid();

        private string _titulo = string.Empty;

        private string _chamada = string.Empty;

        private string _descricao = string.Empty;

        private SmartDate _dataInicio = new SmartDate(false);

        private SmartDate _dataFinal = new SmartDate(false);

        private bool _valido = false;

        private string _imagem = string.Empty;

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid Id
        {
            get
            {
                CanReadProperty("Id", true);
                return _id;
            }
        }

        public string Titulo
        {
            get
            {
                CanReadProperty("Titulo", true);
                return _titulo;
            }
        }

        public string Chamada
        {
            get
            {
                CanReadProperty("Chamada", true);
                return _chamada;
            }
        }

        public string ChamadaLimite
        {
            get
            {
                CanReadProperty("ChamadaLimite", true);
                if (_chamada.Length > 100)
                    return _chamada.Substring(0, 99) + " (...)";
                return _chamada;
            }
        }

        public string Descricao
        {
            get
            {
                CanReadProperty("Descricao", true);
                return _descricao;
            }
        }

        public DateTime DataInicio
        {
            get
            {
                CanReadProperty("DataInicio", true);
                return _dataInicio.Date;
            }
        }

        public string DataInicioString
        {
            get
            {
                CanReadProperty("DataInicioString", true);
                return _dataInicio.Text;
            }
        }

        public DateTime DataFinal
        {
            get
            {
                CanReadProperty("DataFinal", true);
                return _dataFinal.Date;
            }
        }

        public string DataFinalString
        {
            get
            {
                CanReadProperty("DataFinalString", true);
                return _dataFinal.Date.ToString("dd/MM/yyyy");
            }
        }

        public string Imagem
        {
            get
            {
                CanReadProperty("Imagem", true);
                return _imagem;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in NoticiaChildInfo
            //AuthorizationRules.AllowRead("Id", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Titulo", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Chamada", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("Descricao", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataInicio", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataInicioString", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataFinal", "NoticiaChildInfoReadGroup");
            //AuthorizationRules.AllowRead("DataFinalString", "NoticiaChildInfoReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static NoticiaChildInfo GetNoticiaChildInfo(SafeDataReader dr)
        {
            return new NoticiaChildInfo(dr);
        }

        private NoticiaChildInfo(SafeDataReader dr)
        {
            Fetch(dr);
        }
        #endregion //Factory Methods


        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);

        }

        private void FetchObject(SafeDataReader dr)
        {
            _id = dr.GetGuid("Id");
            _titulo = dr.GetString("Titulo");
            _chamada = dr.GetString("Chamada");
            _descricao = dr.GetString("Descricao");
            _dataInicio = dr.GetSmartDate("DataInicio", _dataInicio.EmptyIsMin);
            _dataFinal = dr.GetSmartDate("DataFinal", _dataFinal.EmptyIsMin);
            _valido = _dataInicio.Date.CompareTo(DateTime.Now) > -1 && _dataFinal.Date.CompareTo(DateTime.Now) < 1;
            _imagem = dr.GetString("Imagem");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
